﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class Register : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = UserName.Text;
            string password = Password.Text;
            string email = Email.Text;
            string gender = Gender_female.Checked ? "1" : "0";
            string sql = "insert into StudentInfo (stu_name,gender,email,[password]) values (@name,@gender,@email,@pwd)";
            SqlParameter[] pars =
            {
                new SqlParameter("@name",name),
                new SqlParameter("@pwd",password),
                new SqlParameter("@email",email),
                new SqlParameter("@gender",gender)
            };

            string constr = "server=.;uid=sa;pwd=123456;database=Student_db";
            SqlConnection con = new SqlConnection(constr);

            try
            {
               con.Open();
               SqlCommand cmd = new SqlCommand(sql,con);
                cmd.Parameters.AddRange(pars);

                int result = cmd.ExecuteNonQuery();
                if (result != -1)
               {
                  Literal1.Text = "注册成功，<a href='Login.aspx'>去登陆</a>";
                }
               else
               {
                   Literal1.Text = "注册失败!";
               }
            }
           catch (Exception ex)
           {
                throw new Exception(ex.Message);
            }
           finally
            {
                if (con!=null)
                {
                   con.Close();
              }
           }


        }
    }
}